package my.diplom.util;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext;
import java.util.List;
import java.util.Properties;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;

import my.diplom.entity.Host;
import my.diplom.entity.Resource;
import my.diplom.entity.Order;
import my.diplom.dao.service.HostService;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 15.08.2009
 * Time: 3:16:08
 * To change this template use File | Settings | File Templates.
 */
public class OrderUtil {

    private static HostService hostService;

    public HostService getHostService() {
        return hostService;
    }

    public void setHostService(HostService hostService) {
        this.hostService = hostService;
    }

    private static final Logger log = Logger.getLogger(OrderUtil.class);

    public static List<Float> getCpuList(ServletContext servletContext) {
        String min = "cpu.min";
        String max = "cpu.max";
        String range = "cpu.range";
        return getProperties(servletContext, min, max, range);

    }

    public static List<Float> getHddList(ServletContext servletContext) {
        String min = "hdd.min";
        String max = "hdd.max";
        String range = "hdd.range";
        return getProperties(servletContext, min, max, range);

    }

    public static List<Float> getRamList(ServletContext servletContext) {
        String min = "ram.min";
        String max = "ram.max";
        String range = "ram.range";
        return getProperties(servletContext, min, max, range);

    }

    private static List<Float> getProperties(ServletContext servletContext, String min, String max, String range) {
        Properties properties = new Properties();
        try {
            InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/resources.properties");
            properties.load(inputStream);
        } catch (IOException e) {
            log.error("Erorr get property file", e);
        }
        Float minValue = Float.parseFloat(properties.getProperty(min));
        Float maxValue = Float.parseFloat(properties.getProperty(max));
        Float rangeValue = Float.parseFloat(properties.getProperty(range));

        List<Float> list = new ArrayList<Float>();

        do {
            list.add(minValue);
            minValue += rangeValue;
        } while (minValue <= maxValue);
        return list;
    }

    public static void setAppropriateHosts(List<Order> orders, List<Host> hosts) {

        for (Order o : orders) {
            for (Host h : hosts) {
                boolean appropriate = true;
                if (o.getCpu() > HostUtil.getFreeCpu(h)) appropriate = false;
                if (o.getHdd() > HostUtil.getFreeHdd(h)) appropriate = false;
                if (o.getRam() > HostUtil.getFreeCpu(h)) appropriate = false;
                if (appropriate) o.getAppropriateHosts().add(h.getHostName());
            }
        }

    }


}
